<?php

namespace YCommerce\GlobalBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Produit
 *
 * @ORM\Table(name="produit")
 * @ORM\Entity(repositoryClass="YCommerce\GlobalBundle\Entity\ProduitRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Produit
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var integer
     *
     * @ORM\Column(name="prix", type="decimal")
     */
    private $prix;

    /**
     * @var string
     *
     * @ORM\Column(name="path", type="string", length=255, nullable=true)
     */
    private $path;

    /**
     * @var integer
     *
     * @ORM\Column(name="age_min", type="integer", nullable=true)
     */
    private $ageMin;

    /**
     * @var integer
     *
     * @ORM\Column(name="age_max", type="integer", nullable=true)
     */
    private $ageMax;

    /**
     * @var string
     *
     * @ORM\Column(name="sex", type="string", length=255)
     */
    private $sex;
    
    /**
     * @ORM\ManyToMany(targetEntity="YCommerce\GlobalBundle\Entity\Humeur", cascade={"persist"})
     * @ORM\JoinTable(name="humeur_produit")
     */
    private $humeurs;
    
    /**
     * @ORM\ManyToOne(targetEntity="YCommerce\GlobalBundle\Entity\Gamme")
     * @ORM\JoinColumn(nullable=false)
     */
    private $gamme;
    
    /**
     * @ORM\ManyToMany(targetEntity="YCommerce\GlobalBundle\Entity\Interet", cascade={"persist"})
     * @ORM\JoinTable(name="interet_produit")
     */
    private $interets;
    
    /**
     * Unmapped property to handle file uploads
     * @Assert\File(maxSize="6000000")
     */
    private $file;
    private $temp;
    
    /**
     * @ORM\Column(name="updated", type="datetime", nullable=true)
     */
    private $updated;
    
    /**
     * @ORM\Column(name="hitssimple", type="integer", nullable=true)
     */
    private $hitsSimple;
    
    /**
     * @ORM\OneToMany(targetEntity="YCommerce\GlobalBundle\Entity\Hit", mappedBy="product", cascade={"persist"})
     * @ORM\JoinTable(name="produit_hit")
     */
    private $hits;
    
    /**
     * Constructor
     */
    public function __construct() 
    {
        $this->humeurs = new \Doctrine\Common\Collections\ArrayCollection();
        $this->interets = new \Doctrine\Common\Collections\ArrayCollection();
        $this->hits = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    public function addSimpleHit() {
        $this->hitsSimple++;
    }
    
    /**
     * Add interets
     *
     * @param \YCommerce\GlobalBundle\Entity\Interet $interets
     * @return Produit
     */
    public function addHit(\YCommerce\GlobalBundle\Entity\Hit $hit)
    {
        $this->hits[] = $hit;

        return $this;
    }

    /**
     * Remove interets
     *
     * @param \YCommerce\GlobalBundle\Entity\Interet $interets
     */
    public function removeHit(\YCommerce\GlobalBundle\Entity\Hit $hit)
    {
        $this->hits->removeElement($hit);
    }

    /**
     * Get interets
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getHits()
    {
        return $this->hits;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Produit
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }
    
    public function setUpdated(\DateTime $d) {
        $this->updated = $d;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set prix
     *
     * @param integer $prix
     * @return Produit
     */
    public function setPrix($prix)
    {
        $this->prix = $prix;

        return $this;
    }

    /**
     * Get prix
     *
     * @return integer 
     */
    public function getPrix()
    {
        return $this->prix;
    }

    /**
     * Set urlImg
     *
     * @param string $urlImg
     * @return Produit
     */
    public function setUrlImg($urlImg)
    {
        $this->urlImg = $urlImg;

        return $this;
    }

    /**
     * Get urlImg
     *
     * @return string 
     */
    public function getUrlImg()
    {
        return $this->urlImg;
    }

    /**
     * Set ageMin
     *
     * @param integer $ageMin
     * @return Produit
     */
    public function setAgeMin($ageMin)
    {
        $this->ageMin = $ageMin;

        return $this;
    }

    /**
     * Get ageMin
     *
     * @return integer 
     */
    public function getAgeMin()
    {
        return $this->ageMin;
    }

    /**
     * Set ageMax
     *
     * @param integer $ageMax
     * @return Produit
     */
    public function setAgeMax($ageMax)
    {
        $this->ageMax = $ageMax;

        return $this;
    }

    /**
     * Get ageMax
     *
     * @return integer 
     */
    public function getAgeMax()
    {
        return $this->ageMax;
    }

    /**
     * Set sex
     *
     * @param string $sex
     * @return Produit
     */
    public function setSex($sex)
    {
        $this->sex = $sex;

        return $this;
    }

    /**
     * Get sex
     *
     * @return string 
     */
    public function getSex()
    {
        return $this->sex;
    }

    /**
     * Set humeur
     *
     * @param \YCommerce\GlobalBundle\Entity\Humeur $humeur
     * @return Produit
     */
    public function setHumeur(\YCommerce\GlobalBundle\Entity\Humeur $humeur)
    {
        $this->humeur = $humeur;

        return $this;
    }

    /**
     * Get humeur
     *
     * @return \YCommerce\GlobalBundle\Entity\Humeur 
     */
    public function getHumeur()
    {
        return $this->humeur;
    }

    /**
     * Add humeurs
     *
     * @param \YCommerce\GlobalBundle\Entity\Humeur $humeurs
     * @return Produit
     */
    public function addHumeur(\YCommerce\GlobalBundle\Entity\Humeur $humeurs)
    {
        $this->humeurs[] = $humeurs;

        return $this;
    }

    /**
     * Remove humeurs
     *
     * @param \YCommerce\GlobalBundle\Entity\Humeur $humeurs
     */
    public function removeHumeur(\YCommerce\GlobalBundle\Entity\Humeur $humeurs)
    {
        $this->humeurs->removeElement($humeurs);
    }

    /**
     * Get humeurs
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getHumeurs()
    {
        return $this->humeurs;
    }

    /**
     * Set gamme
     *
     * @param \YCommerce\GlobalBundle\Entity\Gamme $gamme
     * @return Produit
     */
    public function setGamme(\YCommerce\GlobalBundle\Entity\Gamme $gamme)
    {
        $this->gamme = $gamme;

        return $this;
    }

    /**
     * Get gamme
     *
     * @return \YCommerce\GlobalBundle\Entity\Gamme 
     */
    public function getGamme()
    {
        return $this->gamme;
    }

    /**
     * Add interets
     *
     * @param \YCommerce\GlobalBundle\Entity\Interet $interets
     * @return Produit
     */
    public function addInteret(\YCommerce\GlobalBundle\Entity\Interet $interets)
    {
        $this->interets[] = $interets;

        return $this;
    }

    /**
     * Remove interets
     *
     * @param \YCommerce\GlobalBundle\Entity\Interet $interets
     */
    public function removeInteret(\YCommerce\GlobalBundle\Entity\Interet $interets)
    {
        $this->interets->removeElement($interets);
    }

    /**
     * Get interets
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getInterets()
    {
        return $this->interets;
    }

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
        // check if we have an old image path
        if (isset($this->path)) {
            // store the old name to delete after the update
            $this->temp = $this->path;
            $this->path = null;
        } else {
            $this->path = 'initial';
        }
    }

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }
    
    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            // do whatever you want to generate a unique name
            $filename = sha1(uniqid(mt_rand(), true));
            $this->path = $filename.'.'.$this->getFile()->guessExtension();
        }
    }
    
    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->getFile()) {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->getFile()->move($this->getUploadRootDir(), $this->path);

        // check if we have an old image
        if (isset($this->temp)) {
            // delete the old image
            unlink($this->getUploadRootDir().'/'.$this->temp);
            // clear the temp image path
            $this->temp = null;
        }
        $this->file = null;
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }
    
    public function getAbsolutePath()
    {
        return null === $this->path
            ? null
            : $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return 'uploads/documents';
    }
}
